/**
 * Copyright (c) Daniel Centore
 *
 * ex: set filetype=java expandtab tabstop=4 shiftwidth=4 :
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation.
 */
package ronp.anim.gui.tools;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;

import ronp.anim.data.filmstrip.Animation;
import ronp.anim.gui.Gui;
import ronp.anim.gui.OptionsDialog;
import ronp.anim.gui.SelectedState;
import ronp.anim.gui.tools.drawing.LineDrawer;
import ronp.anim.gui.tools.drawing.SimpleImageDrawer;
import ronp.anim.gui.tools.transforms.MoveTransform;
import ronp.anim.gui.viewer.AnimViewer;

public class AnimMenu extends JMenuBar
{
	private static final long serialVersionUID = 1L;

	public AnimMenu(final AnimViewer viewer, final Animation anim, final SelectedState state, final Gui gui)
	{
		// File Menu //
		JMenu file = new JMenu("File");
		this.add(file);

		// Edit Menu //
		JMenu edit = new JMenu("Edit");
		JMenuItem undo = new JMenuItem("Undo");
		undo.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				gui.getUndoController().undo();
			}
		});
		edit.add(undo);

		JMenuItem redo = new JMenuItem("Redo");
		redo.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				gui.getUndoController().redo();
			}
		});
		edit.add(redo);

		edit.add(new JSeparator());

		JMenuItem options = new JMenuItem("Options");
		options.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				new OptionsDialog(viewer, anim, state, gui).setVisible(true);
			}
		});
		edit.add(options);
		this.add(edit);

		// Tools Menu //
		JMenu tools = new JMenu("Tools");
		JMenuItem line = new JMenuItem("Line");
		line.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				viewer.setTool(new LineDrawer(viewer, anim, state, gui.getUndoController()));
			}
		});
		tools.add(line);

		JMenuItem simpImage = new JMenuItem("Simple Image");
		simpImage.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				viewer.setTool(new SimpleImageDrawer(viewer, anim, state, gui.getUndoController()));
			}
		});
		tools.add(simpImage);

		this.add(tools);

		// Transforms Menu //

		JMenu transforms = new JMenu("Transforms");
		JMenuItem move = new JMenuItem("Move Grips");
		move.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				viewer.setTool(new MoveTransform(viewer, anim, state, gui.getUndoController()));
			}
		});
		transforms.add(move);
		this.add(transforms);

		// Preview Menu //
		JMenu preview = new JMenu("Preview");
		JMenuItem play = new JMenuItem("Play");
		play.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				new Thread()
				{
					@Override
					public void run()
					{
						for (int i = 0; i < anim.countFrames(); i++)
						{
							state.setActiveFrame(i);
							anim.repaint();
							try
							{
								Thread.sleep(1000 / anim.getFps());
							} catch (InterruptedException e1)
							{
								e1.printStackTrace();
							}

						}
					}
				}.start();
			}
		});
		preview.add(play);

		JMenuItem play2 = new JMenuItem("Play Thrice");
		play2.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				new Thread()
				{
					@Override
					public void run()
					{
						for (int k = 0; k < 3; k++)
							for (int i = 0; i < anim.countFrames(); i++)
							{
								state.setActiveFrame(i);
								anim.repaint();
								try
								{
									Thread.sleep(1000 / anim.getFps());
								} catch (InterruptedException e1)
								{
									e1.printStackTrace();
								}

							}
					}
				}.start();
			}
		});
		preview.add(play2);

		this.add(preview);

		// Help Menu //
		JMenu help = new JMenu("Help");
		JMenuItem about = new JMenuItem("About");
		about.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				JOptionPane.showMessageDialog(gui, "Created by Daniel Centore", "About", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		help.add(about);
		this.add(help);

	}

}
